﻿document.addEventListener("DOMContentLoaded", function () {
    const form = document.getElementById("registerForm");
    const errorDiv = document.getElementById("errorMsg");

    form.addEventListener("submit", function (event) {
        const userName = document.getElementById("inputUserName").value.trim();
        const password = document.getElementById("inputPassword").value.trim();
        const email = document.getElementById("inputEmail").value.trim();
        const fullName = document.getElementById("inputFullName").value.trim();
        const phoneNo = document.getElementById("inputPhoneNo").value.trim();

        function showError(message) {
            errorDiv.textContent = message;
            errorDiv.style.display = "block";
            // 3秒后自动隐藏
            setTimeout(() => errorDiv.style.display = "none", 3000);
        }

        // 校验逻辑
        if (!userName || !password || !email || !fullName || !phoneNo) {
            event.preventDefault();
            showError("请完整填写所有信息！");
            return;
        }

        const usernamePattern = /^[a-zA-Z0-9_.@-]{3,20}$/;
        if (!usernamePattern.test(userName)) {
            event.preventDefault();
            showError("用户名仅允许字母、数字和符号(_ . - @)，长度3-20位！");
            return;
        }

        const emailPattern = /^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$/;
        if (!emailPattern.test(email)) {
            event.preventDefault();
            showError("请输入有效的邮箱地址！");
            return;
        }

        const phonePattern = /^1[3-9]\d{9}$/;
        if (!phonePattern.test(phoneNo)) {
            event.preventDefault();
            showError("请输入有效的手机号！");
            return;
        }

        if (password.length < 6) {
            event.preventDefault();
            showError("密码长度至少为6位！");
            return;
        }
    });
});